package com.fordmotor.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;

@Configuration
@Data
@Component
public class BaseConfig {


    private static String appId;

    private static String secret;

    private static String redirectUri;

    private static String authorizedUrl;

    private static String accessToken;

    private static String userinfo;

    private static String code;
    private static String openId;
    @Value("${appid}")
    public void setAppId(String appId) {
        BaseConfig.appId = appId;
    }
    @Value("${secret}")
    public void setSecret(String secret) {
        BaseConfig.secret = secret;
    }
    @Value("${redirectUri}")
    public void setRedirectUri(String redirectUri) {
        BaseConfig.redirectUri = redirectUri;
    }
    @Value("${authorizedUrl}")
    public void setAuthorizedUrl(String authorizedUrl) {
        BaseConfig.authorizedUrl = authorizedUrl;
    }
    @Value("${access_token}")
    public void setAccessToken(String accessToken) {
        BaseConfig.accessToken = accessToken;
    }
    @Value("${userinfo}")
    public void setUserinfo(String userinfo) {
        BaseConfig.userinfo = userinfo;
    }

    public static String getOpenId() {
        return openId;
    }
    @Value("${openId}")
    public void setOpenId(String openId) {
        BaseConfig.openId = openId;
    }

    public static String getCode() {
        return code;
    }
    @Value("${code}")
    public void setCode(String code) {
        BaseConfig.code = code;
    }

    public static String getAppId() {
        return BaseConfig.appId;
    }

    public static String getSecret() {
        return BaseConfig.secret;
    }

    public  static String getRedirectUri() {
        return BaseConfig.redirectUri;
    }




    /**
     * 生成授权登录
     */
    public static String getAuthorizedUrl() {
        return BaseConfig.authorizedUrl.replace("APPID", BaseConfig.appId).replace("REDIRECT_URI", URLEncoder.encode(BaseConfig.redirectUri));
    }

    /**
     * 获取code后，获取access_token
     * @param code
     * @return
     */
    public static String getAccessTokenUrl(String code) {
        return BaseConfig.accessToken.replace("APPID", BaseConfig.appId).replace("SECRET", BaseConfig.secret).replace("CODE", code);
    }

    /**
     * 获取用户
     * @param accessToken
     * @param openId
     * @return
     */
    public static String getUserInfo(String accessToken, String openId) {
        return BaseConfig.userinfo.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
    }

}
